package com.cw.jeeyt.controller.api;

import java.util.ArrayList;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import com.cw.jeeyt.api.dto.response.JsonResultDto;
import com.cw.jeeyt.common.DataTable;
import com.cw.jeeyt.common.ResultData;
import com.cw.jeeyt.common.enums.EnumOperateFlag;
import com.cw.jeeyt.common.web.BaseController;
import com.cw.jeeyt.service.bo.api.ApiFacadeProjectInfoBo;
import com.cw.jeeyt.service.intf.api.ApiFacadeProjectInfoService;
import com.cw.jeeyt.service.directive.PageDictDirective;
import com.cw.jeeyt.service.directive.PermissionDirective;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.common.utils.GsonUtil;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * api接口文档所在项目控制层
 *
  * @author chenw
 * @version 1.0.0
 */
@Controller
@RequestMapping("/apiFacadeProjectInfo")
public class ApiFacadeProjectInfoController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(ApiFacadeProjectInfoController.class);
    @Resource
    private ApiFacadeProjectInfoService apiFacadeProjectInfoService;

    /**
     * 跳转到api接口文档所在项目列表查询页面
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/query")
    public ModelAndView query() throws Exception {

        ModelAndView mv = new ModelAndView("api/apiFacadeProjectInfo_index");
        mv.addObject("permission", new PermissionDirective());
        mv.addObject("PageDict", new PageDictDirective());
        return mv;
    }

    /**
     * 加载DataTable列表数据
     *
     * @param dataTable 请求数据
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/listApiFacadeProjectInfo")
    public ResultData listApiFacadeProjectInfo(@RequestBody DataTable dataTable)
    throws Exception {

        ApiFacadeProjectInfoBo apiFacadeProjectInfoBo = GsonUtil.json2Obj(
            GsonUtil.objectToJsonString(dataTable.getQueryCond()), ApiFacadeProjectInfoBo.class);
        if (apiFacadeProjectInfoBo == null) {
            apiFacadeProjectInfoBo = new ApiFacadeProjectInfoBo();
        }
        PageInfo<ApiFacadeProjectInfoBo> pageInfo = apiFacadeProjectInfoService.queryByPage(apiFacadeProjectInfoBo,
            dataTable.getStart(), dataTable.getLength(), dataTable.getOrder()[0]);
        return ResultData.builder()
            .draw(dataTable.getDraw())
            .data(CollectionUtil.isNotEmpty(pageInfo.getList())?pageInfo.getList():new ArrayList())
            .recordsTotal(pageInfo.getTotal())
            .recordsFiltered(pageInfo.getTotal())
            .build();
    }

    /**
     * 跳转到api接口文档所在项目表单页面
     *
     * @param operation 操作  add 新增 edit修改 view查看
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/to-form")
    public ModelAndView editForm(Long id, String operation) throws Exception {

        ModelAndView mv = new ModelAndView("api/apiFacadeProjectInfo_form");
        ApiFacadeProjectInfoBo apiFacadeProjectInfoBo = null;
        if (EnumOperateFlag.ADD.getCode()
            .equals(operation)) {
            apiFacadeProjectInfoBo = new ApiFacadeProjectInfoBo();
        } else {
            apiFacadeProjectInfoBo = apiFacadeProjectInfoService.getApiFacadeProjectInfoById(id);
        }
        mv.addObject("apiFacadeProjectInfo", apiFacadeProjectInfoBo);
        mv.addObject("operation", operation);
        mv.addObject("PageDict", new PageDictDirective());
        mv.addObject("permission", new PermissionDirective());
        return mv;
    }

    /**
     * 根据操作类型保存api接口文档所在项目数据
     *
     * @param apiFacadeProjectInfoBo
     * @param operation add 新增  edit 修改
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/saveApiFacadeProjectInfo", method = RequestMethod.POST)
    public JsonResultDto saveApiFacadeProjectInfo(ApiFacadeProjectInfoBo apiFacadeProjectInfoBo, String operation) {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            if (operation.equals(EnumOperateFlag.ADD.getCode())) {
                apiFacadeProjectInfoService.insert(apiFacadeProjectInfoBo);
            } else if (operation.equals(EnumOperateFlag.EDIT.getCode())) {
                apiFacadeProjectInfoService.update(apiFacadeProjectInfoBo);
            }
            jsonResultDto.setMsg("保存成功");
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            jsonResultDto.setMsg("保存失败:" + e.getMessage());
            e.printStackTrace();
            logger.error("保存api接口文档所在项目失败：" + e.getMessage());
        }
        return jsonResultDto;
    }

    /**
     * 删除api接口文档所在项目
     *
     * @param apiFacadeProjectInfoBo
     * @throws Exception
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public JsonResultDto delete(ApiFacadeProjectInfoBo apiFacadeProjectInfoBo) throws Exception {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            apiFacadeProjectInfoService.delete(apiFacadeProjectInfoBo);
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultDto.setMsg("删除失败：" + e.getMessage());
            logger.error("删除api接口文档所在项目失败：" + e.getMessage());
        }
        return jsonResultDto;
    }
}
